
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

# 单摆： theta''(t) = - g/l sin(theta),
def deriv(y, t):
    return [y[1], - np.sin(y[0])]

def gun(omega0):
    y0 = [ 0.3, omega0 ]
    t = np.arange(0, 10.1, 0.1)
    sol = odeint( deriv, y0, t )
    return abs(sol[-1,0]+0.31)# 相对目标值的偏离量

from scipy.optimize import fsolve

root = fsolve(gun, [0])
print("root=",root,"gun(root)=",gun(root))

#print("sol = ", sol)
#plt.plot(t, sol[:,0], label=r"$\theta(t)$" )
#plt.plot(t, sol[:,1], label=r"$\theta'(t)$" )
#print("sol[-1:0] = ", sol[-1,0])
#plt.legend()
#plt.xlabel(r"$t(s)$")
#plt.savefig("单摆.png")
#plt.show()
